package com.byit.service.impl;
/**
 * Created by marker on 2018/3/6.
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.plugins.Page;
import com.byit.domain.AppApplication;
import com.byit.domain.ConfigProperties;
import com.byit.domain.vo.AboutUsVo;
import com.byit.domain.vo.ApplicationFigure;
import com.byit.domain.vo.ContactUsVo;
import com.byit.domain.vo.IntroduceVo;
import com.byit.mapper.AppApplicationMapper;
import com.byit.mapper.ConfigPropertiesMapper;
import com.byit.service.ConfigService;
import com.byit.utils.PageUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;


/**
 * 配置服务
 **/
@Slf4j
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    private ConfigPropertiesMapper configMapper;
    @Autowired
    private AppApplicationMapper appApplicationMapper;


    public ContactUsVo getContactUs(String configKey) {
        ConfigProperties config = new ConfigProperties();
        config.setKey(ContactUsVo.CONFIG_KEY);
        config = configMapper.selectOne(config);
        if (config != null) { // 存在
            return JSON.parseObject(config.getValue(), ContactUsVo.class);
        }
        // 配置不存在

        ContactUsVo settings = new ContactUsVo();
        return settings;
    }

    @Override
    public void setContactUs(ContactUsVo contactUsVo) {
        ConfigProperties config = new ConfigProperties();
        config.setKey(ContactUsVo.CONFIG_KEY);
        ConfigProperties configOld = configMapper.selectOne(config);

        String value = JSON.toJSONString(contactUsVo);
        if (configOld != null) { // 存在就更新
            configOld.setValue(value);
            configOld.setUpdateTime(new Date());
            configMapper.updateValue(configOld);
        } else { // 不存在直接新增
            log.warn("config '{}'，not exists.", ContactUsVo.CONFIG_KEY);
            config.setValue(value);
            config.setCreateTime(new Date());
            config.setUpdateTime(new Date());
            configMapper.insert(config);
        }
    }


    @Override
    public void setIntroduce(IntroduceVo introduceVo) {
        ConfigProperties config = new ConfigProperties();
        config.setKey(IntroduceVo.CONFIG_KEY);
        ConfigProperties configOld = configMapper.selectOne(config);

        String value = JSON.toJSONString(introduceVo);
        if (configOld != null) { // 存在就更新
            configOld.setValue(value);
            configOld.setUpdateTime(new Date());
            configMapper.updateValue(configOld);
        } else { // 不存在直接新增
            log.warn("config '{}'，not exists.", IntroduceVo.CONFIG_KEY);
            config.setValue(value);
            config.setCreateTime(new Date());
            config.setUpdateTime(new Date());
            configMapper.insert(config);
        }

    }

    @Override
    public IntroduceVo getIntroduce(String configKey) {
        ConfigProperties config = new ConfigProperties();
        config.setKey(IntroduceVo.CONFIG_KEY);
        config = configMapper.selectOne(config);
        if (config != null) { // 存在
            return JSON.parseObject(config.getValue(), IntroduceVo.class);
        }
        // 配置不存在

        IntroduceVo settings = new IntroduceVo();
        return settings;
    }

    @Override
    public void setApplicationFigure(ApplicationFigure applicationFigure) {

        ConfigProperties config = new ConfigProperties();
        config.setKey(applicationFigure.CONFIG_KEY + applicationFigure.getAppId());
        ConfigProperties configOld = configMapper.selectOne(config);
        String value = JSON.toJSONString(applicationFigure);
        if (configOld != null) { // 存在就更新
            configOld.setValue(value);
            configOld.setUpdateTime(new Date());
            configMapper.updateValue(configOld);
        } else { // 不存在直接新增
            log.warn("config '{}'，not exists.", ApplicationFigure.CONFIG_KEY + applicationFigure.getAppId());
            config.setValue(value);
            config.setCreateTime(new Date());
            config.setUpdateTime(new Date());
            configMapper.insert(config);
        }

    }

    @Override
    public ApplicationFigure getApplicationFigure(String configKey) {
        ConfigProperties config = new ConfigProperties();
        config.setKey(configKey);
        config = configMapper.selectOne(config);
        if (config != null) { // 存在
            ApplicationFigure applicationFigure = JSON.parseObject(config.getValue(), ApplicationFigure.class);
            AppApplication application = appApplicationMapper.selectById(applicationFigure.getAppId());
            applicationFigure.setAppName(application.getName());
            return applicationFigure;
        }
        // 配置不存在

        ApplicationFigure settings = new ApplicationFigure();
        return settings;
    }

    @Override
    public Page<JSONObject> getApplicationFigureList(Page<AppApplication> pageInfo, Map<String, Object> map) {
        List<ConfigProperties> list = this.configMapper.selectDataPage(map);
        List<ApplicationFigure> applicationFigures = new ArrayList<ApplicationFigure>();
        for (ConfigProperties configProperties : list) {
            ApplicationFigure applicationFigure = JSON.parseObject(configProperties.getValue(), ApplicationFigure.class);
            AppApplication application = appApplicationMapper.selectById(applicationFigure.getAppId());
            applicationFigure.setAppName(application.getName());
            applicationFigures.add(applicationFigure);
        }
        Page page = PageUtil.transform(pageInfo);
        page.setRecords(applicationFigures);

        return page;
    }

    @Override
    public void delApplicationFigure(String configKey) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("key", configKey);
        ConfigProperties config = new ConfigProperties();
        config.setKey(configKey);
        config = configMapper.selectOne(config);
        configMapper.deleteById(config.getId());
    }

    @Override
    public AboutUsVo getAboutUs() {
        ConfigProperties config = new ConfigProperties();
        config.setKey(AboutUsVo.CONFIG_KEY);
        config = configMapper.selectOne(config);
        if (config != null) { // 存在
            return JSON.parseObject(config.getValue(), AboutUsVo.class);
        }
        // 配置不存在

        AboutUsVo settings = new AboutUsVo();
        return settings;
    }

    @Override
    public void setAboutUs(AboutUsVo aboutUsVo) {

        ConfigProperties config = new ConfigProperties();
        config.setKey(AboutUsVo.CONFIG_KEY);
        ConfigProperties configOld = configMapper.selectOne(config);

        String value = JSON.toJSONString(aboutUsVo);
        if (configOld != null) { // 存在就更新
            configOld.setValue(value);
            configOld.setUpdateTime(new Date());
            configMapper.updateValue(configOld);
        } else { // 不存在直接新增
            log.warn("config '{}'，not exists.", AboutUsVo.CONFIG_KEY);
            config.setValue(value);
            config.setCreateTime(new Date());
            config.setUpdateTime(new Date());
            configMapper.insert(config);
        }

    }


}
